package main;

import main.Link.SQLLink;
import main.SQL.MsSQLConnect;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class XMLTaskRscr {

    //Добавление новой конфигурации сервера в конец XML-файла
    public void addConfig(ArrayList<SQLLink> arrayList, String projectName) {

        //Рут XML
        org.jdom2.Element serverList = new org.jdom2.Element("TaskRscr");
        Document document = new Document(serverList);

        Connection connSQL = MsSQLConnect.ConnectDB("jdbc:sqlserver://ssqlphn1d;database=Factage;user=sa1;password=11111Q!;");
        try {
            CallableStatement stmt = connSQL.prepareCall("{call getXMLLink(?, ?)}");
            stmt.setString( 1, MainApp.curProject.getDbName() );
            stmt.setInt(2, MainApp.curProject.getId());
            ResultSet rs = stmt.executeQuery();
            HashMap <String, BigDecimal> coeffs  = new HashMap<>();
            HashMap <String, BigDecimal> sums = new HashMap<>();

            while (rs.next()) {
                String smrId = rs.getString("SMETA_ASU_ID") + "-" + rs.getString("LINE_NUM");
                BigDecimal pvVol = new BigDecimal(0);

                coeffs.put(smrId, (coeffs.containsKey(smrId) ? coeffs.get(smrId) : new BigDecimal(0.0)).add(new BigDecimal(rs.getDouble("WORK_COEFF")).setScale(6, RoundingMode.HALF_UP)));
                BigDecimal physBD = new BigDecimal(rs.getDouble("LINE_PHYS")).setScale(6, RoundingMode.HALF_UP);

                if (coeffs.get(smrId).compareTo(new BigDecimal(0.99999)) == 1 ) {
                    pvVol = physBD.subtract((sums.containsKey(smrId) ? sums.get(smrId) : new BigDecimal(0)));
                } else {
                    sums.put(smrId, (sums.containsKey(smrId)? sums.get(smrId) : new BigDecimal(0) ).add(new BigDecimal(rs.getDouble("LINE_PHYS_PLAN")).setScale(6, RoundingMode.HALF_UP)));
                }

                Element link = new Element("Link");
                link.addContent(new Element("SMETA_NUM").setText(rs.getString("SMETA_NUM")));
                link.addContent(new Element("SMETA_ASU_ID").setText(rs.getString("SMETA_ASU_ID")));
                link.addContent(new Element("LINE_NUM").setText(rs.getString("LINE_NUM")));
                link.addContent(new Element("LINE_PHYS").setText(physBD.toString()));
                link.addContent(new Element("LINE_PHYS_PLAN").setText((pvVol.signum() != 0 ? pvVol : new BigDecimal(rs.getDouble("LINE_PHYS_PLAN")).setScale(6, RoundingMode.HALF_UP)).toString()));

//                link.addContent(new Element("coeffsBD").setText(coeffs.get(smrId).toString()));
//                link.addContent(new Element("sumsBD").setText(sums.containsKey(smrId)?sums.get(smrId).toString(): ""));
//                link.addContent(new Element("pvVolBD").setText(pvVol.toString()));

                link.addContent(new Element("LINE_PHYS_FACT").setText(new BigDecimal(rs.getDouble("LINE_PHYS_FACT")).setScale(6, RoundingMode.HALF_UP).toString()));
                link.addContent(new Element("LINE_PHYS_MEAS").setText(rs.getString("LINE_PHYS_MEAS")));
                link.addContent(new Element("LINE_COST_TYPE").setText(rs.getString("LINE_COST_TYPE")));
                link.addContent(new Element("LINE_COST_PLAN").setText(new BigDecimal(rs.getDouble("LINE_COST_PLAN")).setScale(6, RoundingMode.HALF_UP).toString()));
                link.addContent(new Element("LINE_COST_FACT").setText(new BigDecimal(rs.getDouble("LINE_COST_FACT")).setScale(6, RoundingMode.HALF_UP).toString()));
                link.addContent(new Element("PRIMA_PROJECT_ID").setText(rs.getString("PRIMA_PROJECT_ID")));
                link.addContent(new Element("WORK_ID").setText(rs.getString("WORK_ID")));
                link.addContent(new Element("WORK_CODE").setText(rs.getString("WORK_CODE")));
                link.addContent(new Element("WORK_COEFF").setText(new BigDecimal(rs.getDouble("WORK_COEFF")).setScale(6, RoundingMode.HALF_UP).toString()));
                document.getRootElement().addContent(link);
            }

            rs.close();
            stmt.close();

            connSQL.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            FileWriter fw = new FileWriter("LinkRscr.xml");
            outputter.output(document, fw);
            fw.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }
}
